import { usePlottingMapStore } from '#/store/modules/mapStore.js';

// 将所输入颜色一致转换成16进制
function colorProcessed(input) {
  if (typeof input !== 'string') return null;

  // 如果已经是 hex 格式，直接返回
  if (input.startsWith('#')) {
    return input;
  }

  // 标准 CSS 颜色名到十六进制的映射
  const colorNameMap = {
    red: '#ff0000',
    blue: '#0000ff',
    green: '#008000',
    black: '#000000',
    white: '#ffffff',
    yellow: '#ffff00',
    orange: '#ffa500',
    purple: '#800080',
    pink: '#ffc0cb',
    gray: '#808080',
  };

  const lower = input.toLowerCase();
  if (colorNameMap[lower]) {
    return colorNameMap[lower];
  }

  // 未知颜色
  console.warn(`colorProcessed: 未识别的颜色名 "${input}"`);
  return null;
}

export function intelligentDraw(code, coordinates, color, angle, text) {
  const processedColor = colorProcessed(color);
  PlottingAppTool.prototype.intelligentDraw(
    code,
    coordinates,
    processedColor,
    angle,
    text,
  );
}

// 返回SML文档
export function formatSML() {
  const plottingSerializer = new PlottingSerializer(document.plotting);
  //   debugger;
  plottingSerializer.formatXML();
  const SMLDoc = plottingSerializer.scene_doc;
  return SMLDoc;
}

export function parseSMLString(smlString) {
  const plottingSerializer = new PlottingSerializer(document.plotting);
  plottingSerializer.parseXML_Str(smlString);
}

export function deleteNode(nodeId) {
  const mapStore = usePlottingMapStore();
  // mapstore里备份的layer
  const layer = mapStore.findParentById(nodeId);
  const layerId = layer.id;
  const plottingLayer = document.plotting.getLayerById(layerId);
  const plottingNode = plottingLayer.getNodeById(nodeId);
  plottingLayer.remove(plottingNode, false);
}

// 设置单标绘可见性
export function setVisibility(nodeId, visibility) {
  document.plotting.setNodeVisibility(nodeId, visibility);
}

export function setLayerVisibility(layerId, visibility) {
  document.plotting.setLayerVisibility(layerId, visibility);
}

export function simulateCommanderDecision() {
  // 两条作战分界线
  intelligentDraw(
    26_400,
    [125.7331, 55.0636, 95.4471, 46.4229, 69.5721, 37.6332],
    'red',
    0,
    '282U',
  );
  intelligentDraw(
    26_400,
    [133.5653, 41.7266, 101.5112, 28.0523, 77.2293, 16.6001],
    'red',
    0,
    '246U',
  );
  // 四条堑壕
  intelligentDraw(
    15_200,
    [93.1479, 44.709, 94.5989, 44.1163, 95.1104, 42.4274],
    'blue',
    0,
    '',
  );

  intelligentDraw(
    15_200,
    [98.0611, 41.6663, 99.6691, 40.9692, 100.1496, 39.1584],
    'blue',
    0,
    '',
  );

  intelligentDraw(
    15_200,
    [103.5547, 38.3494, 105.1502, 37.7819, 105.721, 35.9576],
    'blue',
    0,
    '',
  );

  intelligentDraw(
    15_200,
    [108.0872, 35.0706, 109.9708, 34.2072, 110.6281, 32.6945],
    'blue',
    0,
    '',
  );

  // 三条电网
  intelligentDraw(15_805, [99.2074, 43.937, 96.6258, 46.6121], 'blue', 0, '');
  intelligentDraw(15_805, [107.537, 38.7275, 103.1885, 41.9889], 'blue', 0, '');
  intelligentDraw(
    15_805,
    [113.8225, 33.1454, 111.2442, 35.7923],
    'blue',
    0,
    '',
  );

  // 两个雷场
  intelligentDraw(
    17_400,
    [107.9247, 38.426, 111.5489, 36.14, 111.5146, 38.8047],
    'blue',
    0,
    '',
  );
  intelligentDraw(17_400, [99.8696, 44.433, 102.4765, 41.8527], 'blue', 0, '');

  // 敌军炮兵群
  intelligentDraw(29_500, [81.9907, 39.348, 86.6624, 31.3327], 'blue', 0, '');

  // 我军步兵战车与坦克进攻队形
  intelligentDraw(
    30_002,
    [108.1608, 46.1817, 106.0909, 46.8614, 105.1703, 48.8105],
    'red',
    0,
    '',
  );

  // 我军主攻
  intelligentDraw(
    25_200,
    [
      99.2381, 44.3002, 102.164, 41.6632, 94.3346, 35.1393, 86.2913, 29.4972,
      79.0088, 27.2565,
    ],
    'red',
    0,
    '',
  );

  // 敌军指挥所
  intelligentDraw(2800, [75.1321, 26.0571], 'blue', 0, '');

  // 我军先遣部队
  intelligentDraw(28_501, [117.4938, 40.4274], 'red', 0, '');

  // 我军炮兵群阵地
  intelligentDraw(
    29_500,
    [116.4393, 47.9719, 122.3276, 42.2096, 124.5126, 47.6967],
    'red',
    0,
    '',
  );

  // 我军机降部队
  intelligentDraw(28_800, [128.3278, 42.4221], 'red', 0, '');
}
